package com.affablebean.model.dal;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.affablebean.model.entity.Category;

public class CategoryDAL extends AbstractDAL<Category> {

	public CategoryDAL() {
		super("CATEGORIES");
	}

	@Override
	public boolean insertEntity(Category entity) throws SQLException {
		boolean result = false;
		try (Connection connection = createNewConnection()) {
			final String sqlCmd = "INSERT INTO categories VALUES (category_seq.NEXTVAL, ?)";
			try (PreparedStatement statement = connection.prepareStatement(sqlCmd, new String[] { "category_id" })) {
				statement.setString(1, entity.getName());
				int affectedRows = statement.executeUpdate();
				result = affectedRows > 0;
				if (result) {
					try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
						if (generatedKeys.next()) {
							entity.setId(generatedKeys.getInt(1));
						}
					}
				}
			}
		}
		return result;
	}

	@Override
	public boolean updateEntity(Category entity) throws SQLException {
		boolean result = false;
		try (Connection connection = createNewConnection()) {
			final String sqlCmd = "UPDATE categories SET category_name = ? WHERE category_id = ?";
			try (PreparedStatement statement = connection.prepareStatement(sqlCmd)) {
				statement.setString(1, entity.getName());
				statement.setInt(2, entity.getId());
				int affectedRows = statement.executeUpdate();
				result = affectedRows > 0;
			}
		}
		return result;
	}

	@Override
	public boolean deleteEntity(Category entity) throws SQLException {
		boolean result = false;
		try (Connection connection = createNewConnection()) {
			final String sqlCmd = "DELETE FROM categories WHERE category_id = ?";
			try (PreparedStatement statement = connection.prepareStatement(sqlCmd)) {
				statement.setInt(1, entity.getId());
				int affectedRows = statement.executeUpdate();
				result = affectedRows > 0;
			}
		}
		return result;
	}

	@Override
	public List<Category> getAll() throws SQLException {
		List<Category> categories = new ArrayList<Category>();
		try (Connection connection = createNewConnection()) {
			try (Statement statement = connection.createStatement()) {
				final String sqlCmd = "SELECT * FROM categories";
				try (ResultSet resultSet = statement.executeQuery(sqlCmd)) {
					while (resultSet.next()) {
						categories.add(createEntity(resultSet));
					}
				}
			}
		}

		return categories;
	}

	@Override
	public Category findById(int id) throws SQLException {
		Category category = null;
		try (Connection connection = createNewConnection()) {
			final String sqlCmd = "SELECT * FROM categories WHERE category_id = ?";
			try (PreparedStatement statement = connection.prepareStatement(sqlCmd)) {
				statement.setInt(1, id);
				try (ResultSet resultSet = statement.executeQuery()) {
					if (resultSet.next()) category = createEntity(resultSet);
				}
			}

		}
		return category;
	}

	@Override
	Category createEntity(ResultSet resultSet) throws SQLException {
		Category category = new Category();
		category.setId(resultSet.getInt("category_id"));
		category.setName(resultSet.getString("category_name"));
		return category;
	}

}
